/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package plugin;

import static org.openstreetmap.josm.tools.I18n.tr;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.ExtensionFileFilter;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.gui.layer.GpxLayer;
import org.openstreetmap.josm.gui.layer.markerlayer.Marker;
import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
import org.openstreetmap.josm.gui.progress.ProgressMonitor;
import org.openstreetmap.josm.io.FileImporter;
import org.openstreetmap.josm.io.GpxReader;

import plugin.AudioButtonMarker;

/**
 * FileImport extends the JOSM - class FileImporter and
 * handles the import of kOSMik - files.
 * <p>
 * It bases on GpxImporter - class, which is described on:
 * http://josm.openstreetmap.de/browser/josm/trunk/src/org/
 * openstreetmap/josm/io/GpxImporter.java
 * <p>
 * @author Andreas
 */
public class FileImport extends FileImporter{

	static MarkerLayer ml;
	
	/**
	 * Constructor.
	 */
	public FileImport() {
		super(new ExtensionFileFilter("kosmik", "kosmik", tr("kOSMik - files ") + "(*.kosmik)"));
	}
	
	@Override
	public void importData(final File file, ProgressMonitor progressMonitor) throws IOException {
		final String fn = file.getName();
			
		try {
			InputStream is;
			is = new FileInputStream(file);
			
			// Create a GPX - Layer
			GpxReader r = new GpxReader(is);
			r.data.storageFile = file;
			GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
			Main.main.addLayer(gpxLayer);
				
			// Now try to add a marker layer.
			try{
				ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
				Main.main.addLayer(ml);
				try{
					readMediaData(file.toString());
				} catch(IOException e){
					// Do nothing
				}
			} catch(Exception e){
				// Do nothing.
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new IOException(tr("File \"{0}\" does not exist", file.getName()));
		} 
		
	}
	
	/**
	 * ReadMediaData create different markers.
	 * @param file specifies the media information.
	 * @throws IOException if file doesn't exist.
	 */
	public static void readMediaData(String file) throws IOException{
		
		double currentLatitude = 0;
		double currentLongitude = 0;
		
		LatLon ll;
		
		String str = "";
		String latTemp = "";
		String lonTemp = "";
		String filePath = "";
		try {
			BufferedReader in=new BufferedReader(new FileReader(file));
			while(null != (str = in.readLine())){
				if(str.contains("lat=")){
					latTemp = (String) str.subSequence(str.indexOf("lat=")+5, str.indexOf("lon")-2);
					currentLatitude = Double.parseDouble(latTemp);
				}
				if(str.contains("lon=")){
					lonTemp = (String) str.subSequence(str.indexOf("lon=")+5, str.indexOf(">")-1);
					currentLongitude = Double.parseDouble(lonTemp);
				}
				if(str.contains("media")){
					filePath = (String) str.subSequence(str.indexOf("v=")+3, str.indexOf("/>")-2);
					if(filePath.endsWith(".wav") || filePath.endsWith(".mp3")){
						ll = new LatLon(currentLatitude, currentLongitude);
						Marker mr = AudioButtonMarker.create(ll, " ", filePath, ml, 0.0, 0.0);
						ml.data.add(mr);
					}
					if(filePath.endsWith(".avi") || filePath.endsWith(".mp4")){
						ll = new LatLon(currentLatitude, currentLongitude);
						Marker mr = VideoButtonMarker.create(ll, " ", filePath, ml, 0.0, 0.0);
						ml.data.add(mr);
					}
					if(filePath.endsWith(".txt")){
						ll = new LatLon(currentLatitude, currentLongitude);
						Marker mr = TextButtonMarker.create(ll, " ", filePath, ml, 0.0, 0.0);
						ml.data.add(mr);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}

}
